package com.monise.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;

import com.monise.calendario.JDateChooser;
import com.monise.dao.DAO.EntradaCaixaDAO;
import com.monise.dao.DAO.RetiradaCaixaDAO;
import com.monise.dao.DAO.TipoMovimentacaoDAO;
import com.monise.model.EntradaCaixa;
import com.monise.model.RetiradaCaixa;
import com.monise.model.TipoMovimentacao;

public class FrameMovimentacao extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtValor;
	private JTextField txtOutro;
	private final JDateChooser txtDataConsulta = new JDateChooser();
	private JTextArea txtObservacao;
	private JRadioButton rdAcao;
	private JComboBox cboAcao;
	private JRadioButton rdOutro;
	private JRadioButton rdEntrada;
	private JRadioButton rdSaida;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameMovimentacao frame = new FrameMovimentacao();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameMovimentacao() {
		setTitle("Movimenta\u00E7\u00E3o");
		setClosable(true);
		setBounds(100, 100, 667, 413);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Tipo", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		rdEntrada = new JRadioButton("Entrada");
		
		rdSaida = new JRadioButton("Sa\u00EDda");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 225, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(rdEntrada)
					.addGap(32)
					.addComponent(rdSaida, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(46, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 58, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(rdEntrada)
						.addComponent(rdSaida))
					.addContainerGap(8, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Data", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	
		txtDataConsulta.getCalendarButton().setIcon(new ImageIcon(FrameMovimentacao.class.getResource("/com/monise/calendario/JDateChooserIcon.gif")));

		txtDataConsulta.setFont(new Font("Tahoma", Font.PLAIN, 11));
		txtDataConsulta.setBounds(28,25, 135, 25);
		panel_1.add(txtDataConsulta);

		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGap(0, 185, Short.MAX_VALUE)
				.addGap(0, 173, Short.MAX_VALUE)
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGap(0, 58, Short.MAX_VALUE)
				.addGap(0, 31, Short.MAX_VALUE)
		);
		panel_1.setLayout(gl_panel_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Valor", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		txtValor = new JTextField();
		txtValor.setColumns(10);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGap(0, 188, Short.MAX_VALUE)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(19)
					.addComponent(txtValor, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(32, Short.MAX_VALUE))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGap(0, 58, Short.MAX_VALUE)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addComponent(txtValor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Detalhes", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		rdAcao = new JRadioButton("A\u00E7\u00E3o");
		
		cboAcao = new JComboBox();
		
		rdOutro = new JRadioButton("Outro");
		
		txtOutro = new JTextField();
		txtOutro.setColumns(10);
		
		JLabel label = new JLabel("Observa\u00E7\u00E3o:");
		
		txtObservacao = new JTextArea();
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGap(0, 614, Short.MAX_VALUE)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel_3.createSequentialGroup()
							.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
								.addComponent(rdAcao)
								.addComponent(cboAcao, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE))
							.addGap(57)
							.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
								.addComponent(rdOutro)
								.addComponent(txtOutro, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE)))
						.addComponent(label)
						.addComponent(txtObservacao))
					.addContainerGap(177, Short.MAX_VALUE))
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addGap(10)
					.addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE)
						.addComponent(rdAcao)
						.addComponent(rdOutro))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE)
						.addComponent(cboAcao, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtOutro, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtObservacao, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(29, Short.MAX_VALUE))
		);
		panel_3.setLayout(gl_panel_3);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.setIcon(new ImageIcon(FrameMovimentacao.class.getResource("/com/monise/imagens/Save.png")));
		
		JButton btnLimpar = new JButton("Limpar");
		btnLimpar.setIcon(new ImageIcon(FrameMovimentacao.class.getResource("/com/monise/imagens/Trash.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 614, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(16, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(20)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
						.addComponent(panel_1, 0, 0, Short.MAX_VALUE)
						.addComponent(panel, 0, 0, Short.MAX_VALUE))
					.addGap(11)
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(30, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

		try {
			preencherAcao();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		btnSalvar.addActionListener(new SalvarMovimentacao());
		btnLimpar.addActionListener(new Limpar());
		
		rdAcao.addActionListener(new SelecionaAcao());
		rdEntrada.addActionListener(new SelecionaEntrada());
		rdOutro.addActionListener(new SelecionaOutro());
		rdSaida.addActionListener(new SelecionaSaida());

	}
	
	private void entradaCaixa(){
		
		    java.sql.Date data = new java.sql.Date(txtDataConsulta.getDate().getTime());  
		
			EntradaCaixa entradaCaixa = new EntradaCaixa();
			entradaCaixa.setDataEntrada(data);
			entradaCaixa.setObservacao(txtObservacao.getText());
			String valorCaixa = txtValor.getText().replace(",", ".");
			entradaCaixa.setValor(BigDecimal.valueOf(Double.parseDouble(valorCaixa)));
			
			if(rdAcao.isSelected()){
				TipoMovimentacao tipo = (TipoMovimentacao) cboAcao.getSelectedItem();
				entradaCaixa.setTipoMovimentacao(tipo);
				entradaCaixa.setOutro(null);
			}
			
			if(rdOutro.isSelected()){
				entradaCaixa.setTipoMovimentacao(null);
				entradaCaixa.setOutro(txtOutro.getText());
			}
			
			EntradaCaixaDAO entradaDao = new EntradaCaixaDAO();
			try{
				entradaDao.save(entradaCaixa);
				JOptionPane.showMessageDialog(null, "Entrada salvo com sucesso!");
			}catch(Exception ex){
				JOptionPane.showMessageDialog(null, "Ocorreu um erro no momento de salvar!");
			}
	}
	
	private void retiradaCaixa(){
		java.sql.Date data = new java.sql.Date(txtDataConsulta.getDate().getTime());  
		
		RetiradaCaixa retiradaCaixa = new RetiradaCaixa();
		retiradaCaixa.setDataRetirada(data);
		retiradaCaixa.setObservacao(txtObservacao.getText());
		String valorCaixa = txtValor.getText().replace(",", ".");
		retiradaCaixa.setValor(BigDecimal.valueOf(Double.parseDouble(valorCaixa)));
		
		if(rdAcao.isSelected()){
			TipoMovimentacao tipo = (TipoMovimentacao) cboAcao.getSelectedItem();
			retiradaCaixa.setOutro(null);
			retiradaCaixa.setTipoMovimentacao(tipo);
		}
		
		if(rdOutro.isSelected()){
			retiradaCaixa.setOutro(txtOutro.getText());
			retiradaCaixa.setTipoMovimentacao(null);
		}
		
		RetiradaCaixaDAO retiradaDao = new RetiradaCaixaDAO();
		try{
			retiradaDao.save(retiradaCaixa);
			JOptionPane.showMessageDialog(null, "Retirada salvo com sucesso!");
		}catch(Exception ex){
			JOptionPane.showMessageDialog(null, "Ocorreu um erro no momento de salvar!");
		}
	}
	
	private void preencherAcao() throws Exception{
		TipoMovimentacaoDAO tipoDao = new TipoMovimentacaoDAO();
		List<TipoMovimentacao> tipos = tipoDao.getAll();
		
		for(TipoMovimentacao tipo : tipos){
			cboAcao.addItem(tipo);
		}
	}
	
	private class SalvarMovimentacao implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			
			if(rdEntrada.isSelected()){
				entradaCaixa();
			}
			
			if(rdSaida.isSelected()){
				retiradaCaixa();
			}
		}
	}
	
	private class SelecionaEntrada implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			rdSaida.setSelected(false);
		}
	}
	
	private class SelecionaSaida implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			rdEntrada.setSelected(false);
		}
	}
	
	private class SelecionaAcao implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			rdOutro.setSelected(false);
			txtOutro.setEditable(false);
			txtOutro.setEnabled(false);
			cboAcao.setEditable(true);
			cboAcao.setEnabled(true);
		}
	}
	
	private class Limpar implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			limparCampos();
		}
	}
	
	private class SelecionaOutro implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			rdAcao.setSelected(false);
			txtOutro.setEditable(true);
			txtOutro.setEnabled(true);
			cboAcao.setEditable(false);
			cboAcao.setEnabled(false);
		}
	}
	
	private void limparCampos(){
		txtDataConsulta.setDate(null);
		txtObservacao.setText("");
		txtOutro.setText("");
		txtValor.setText("");
	}
}
